home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 157_01 / qed5 < prev    next >
Text File  |  1987-10-10  |  5KB  |  232 lines

  1. /*  VERSION 0002  (DATE: 28/04/87)  (TIME: 22:57)  */
  2. /*
  3.     e (qed) screen editor
  4.  
  5.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  6.  
  7.     August-December 1981
  8.  
  9.     Modified: Aug-Dec    1984:    BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  10.               March        1985:    BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  11.  
  12.     FILE: qed5
  13.  
  14.     FUNCTIONS: readfile, writefile, exists, scans, retag, format
  15.  
  16.     PURPOSE: read and write files
  17.  
  18. */
  19.  
  20. #include "qed.h"
  21. #define    GS    0x1d
  22. #define ESC    0x1b
  23.  
  24. readfile(name)    /* read file 'name' and insert it after cursor*/
  25. char *name;
  26. {
  27.     int c, i, j, k, line, first, res;
  28.     char textb[LLIM];
  29.  
  30.     if (fopen(name,fbuf) == FAIL) {
  31.         if (fopen(strcat(name,defext),fbuf) == FAIL) {
  32.             error("Can't find file");
  33.             name[0]='\0';
  34.             return(FAIL);
  35.             }
  36.         }
  37.     first=YES;
  38.     i=charn; 
  39.     line=cline;
  40.     for (j=charn, k=0; (textb[k]=text[j]); j++, k++);
  41.     do {
  42.         while (i < LLIM && ((c=egetc(fbuf)) != DFAIL) && (c != '\n')
  43.         && (c != ENDFILE))
  44.          text[i++]=c;
  45.         if (text[i-1] == '\r') i--;
  46.         text[i]='\0';
  47.         if (c == ENDFILE) for (k=0; (text[i]=textb[k]) && i < LLIM; i++, k++);
  48.         if (first) {
  49.             altered=YES;
  50.             puttext();
  51.             first=NO;
  52.             res=YES;
  53.             }
  54.         else {
  55.             res=inject(line,text);
  56.             line++;
  57.             }
  58.         i=0;
  59.         } while (c != DFAIL && res != FAIL && c != ENDFILE);
  60.     fclose(fbuf);
  61.     gettext(cline);
  62. }
  63.  
  64. writefile(from,to,name,nametoprint,exiting)
  65. int from, to, exiting;
  66. char *name, *nametoprint;
  67. {
  68.     int l, copying, copysecstart,copybytestart;
  69.     char c, *t, *getline();
  70.  
  71.     puttext();
  72.     if (name[0] <= ' ') {
  73.         error("Bad name");
  74.         return FAIL;
  75.         }
  76.     if (fcreat(name,fbuf) == FAIL) {
  77.         error("Can't write file - directory full?");
  78.         return(FAIL);
  79.         }
  80.     if (expert) xprtmess("Saving ");
  81.     else putmess("Saving ");
  82.     putstr(nametoprint);
  83.     copying=NO;
  84.     if (exiting) to=lastread;
  85.     else if (to == lastl) to=loc(lastl,0);
  86.     for (l=from; l <= to; ) {
  87.         t=getline(l++);
  88.         while (*t) if(eputc(*t++,fbuf) == FAIL) goto diskfull;
  89.         if (l <= to ) {
  90.             if (eputc('\r',fbuf) == FAIL) goto diskfull;
  91.             if (eputc('\n',fbuf) == FAIL) goto diskfull;
  92.             }
  93.         }
  94.     if (exiting && ((lastl == UNKNOWN) || !goteof) ) {
  95.         copybytestart=SECSIZ-(textbuf->_nleft + SECSIZ)%SECSIZ;
  96.         if (eputc('\r',fbuf) == FAIL) goto diskfull;
  97.         if (eputc('\n',fbuf) == FAIL) goto diskfull;
  98.         copying=YES;
  99.         while ( (c=egetc(textbuf)) != ENDFILE && c != DFAIL)
  100.             if (eputc(c,fbuf) == FAIL) goto diskfull;
  101.         }
  102. #if CPM
  103.     if (eputc(ENDFILE,fbuf) == FAIL)goto diskfull;  /*CP/M only*/
  104. #endif
  105.     if (fflush(fbuf) == FAIL) goto diskfull;
  106.     if (fclose(fbuf) == FAIL) {
  107.         error("Can't close file");
  108.         goto reposition;
  109.         }
  110.     if (copying) fclose(textbuf);
  111.     return YES;
  112. diskfull:
  113.     error("Disk full");
  114. reposition:
  115.     if (copying) {
  116.         fclose(fbuf);
  117.         funlink(name);
  118.         textbuf->_nleft=0;
  119.         while (copybytestart--) egetc(textbuf);
  120.         }
  121.     return FAIL;
  122. }
  123.  
  124. exists(name)
  125. char *name;
  126. {
  127.     char c;
  128.  
  129.     c='y';
  130.     if (checkexists(name)) {
  131.         if (expert) xprtmess("OK to replace ");
  132.         else putmess("OK to replace ");
  133.         putstr(name); 
  134.         putstr(" ? ");
  135.         putch((c=getlow()));
  136.         putret(); 
  137.         }
  138.     return c == 'y';
  139. }
  140.  
  141. checkexists(name)    /*return YES if file 'name' exists, else NO */
  142. char *name;
  143. {
  144.     int fd;
  145.     
  146. /*if (dskcheck(setjmp(dskerr)) != 0 || (fd=open(name,0)) == FAIL) return NO;*/
  147.     /* no setjmp just yet */ 
  148.     if ((fd=open(name,0)) == FAIL) return NO;
  149.     close(fd);
  150.     return YES;
  151. }
  152.  
  153. scans(answer,maxlen)
  154. char *answer;
  155. int maxlen;
  156. {
  157.     char c;
  158.     int n, i;
  159.  
  160.     maxlen--;
  161.     n=0;
  162.     while (n < maxlen) {
  163.         switch((c=getscankey())) {
  164.          case BACKSP        :
  165.         case DELLEFT_P    :
  166.             if (n) {
  167.                 putch(BACKSP); 
  168.                 putch(' ');
  169.                 putch(BACKSP);
  170.                 n--; 
  171.                 answer--;
  172.                 }
  173.             break;
  174.         case CR_P        :
  175.         case ESCKEY_P    :
  176.             n=maxlen; 
  177.             break;
  178.         case LEFTKEY_P    :     /* pass this key code thru */
  179.             c = tran[LEFTKEY];
  180.             goto passthru;
  181.         case RETRIEVE_P    :
  182.             /*if (n == 0) {*/
  183.             if (*answer) {
  184.                 while (*answer) {
  185.                     dispch(*answer++);
  186.                     n++;
  187.                     }
  188.                 break;
  189.                 }
  190.             else {
  191.                 c = tran[RETRIEVE];    /*if no answer, display the key code*/
  192.                 goto passthru;
  193.                 }
  194.         case GS        :            /* kludge to permit ESC in find/alter */
  195.             c=ESC;
  196.         default        :   
  197. passthru:    dispch(c);
  198.             *answer++=c; 
  199.             n++;
  200.             break;
  201.             }
  202.         }
  203.     *answer='\0';
  204.     putret();                    /* set PARBIT of c back to 0 to */
  205.     return (c & ~PARBIT);        /* maintain compatibility with other calls*/
  206. }
  207.  
  208. retag(name,tag)    /*puts a new suffix on a file name*/
  209. char *name, *tag;
  210. {
  211.     for (; *name && *name != '.'; name++);
  212.     if (!*name)*name='.';
  213.     for (++name; (*name=*tag); name++, tag++);
  214. }
  215.  
  216. format(name)
  217. char *name;
  218. {
  219.     char tempname[FILELEN], *n, *tn;
  220.  
  221.     if (!*name) return;
  222.     if (*(name+1) == ':') strcpy(tempname,name);
  223.     else {
  224.         tempname[0]=curdsk+'A';
  225.         tempname[1]=':';
  226.         name[FILELEN]='\0';
  227.         strcpy(&tempname[2],name);
  228.         }
  229.     for (n=name,tn=tempname; (*n=toupper(*tn++)); n++) if (*n == '.') break;
  230.     while (*n++) *n=tolower(*tn++);
  231. }
  232.